<?php
namespace Model;
use Think\Model;
//分类模型
class CategoryModel extends Model {
	static public function findChild(&$data, $pid = 0, $col_pid = 'pid') {
	        $rootList = array();
	        foreach ($data as $key => $val) {
	            if ($val[$col_pid] == $pid) {
	                $rootList[]   = $val;
	                unset($data[$key]);
	            }
	        }
	        return $rootList;
	    }
	    
	    /**
	     * 无限分级
	     * @access  public
	     * @param   array     &$data      数据库里取得的结果集 地址引用
	     * @param   integer   $pid        父级id的值
	     * @param   string    $col_id     自增id字段名（对应&$data里的字段名）
	     * @param   string    $col_pid    父级字段名（对应&$data里的字段名）
	     * @param   string    $col_cid    是否存在子级字段名（对应&$data里的字段名）
	     * @return  array     $childs     返回整理好的数组
	     */
	static public function getCat(&$data, $pid = 0, $col_id = 'id', $col_pid = 'pid', $col_cid = 'haschild') {
    	$childs = self::findChild($data, $pid, $col_pid);
    	if (empty($childs)) {
    		return null;
    	}
    	foreach ($childs as $key => $val) {
    		if ($val[$col_cid]) {
    			$treeList = self::getCat($data, $val[$col_id], $col_id, $col_pid, $col_cid);
    			if ($treeList !== null) {
    				$childs[$key]['childs'] = $treeList;
    			}
    		}
    	}
    	return $childs;
    }
}